package it4s.service.api.facebook.core;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Post;
import facebook4j.ResponseList;
import facebook4j.User;
import it4s.service.api.facebook.exceptions.IT4SExceptionService;
import it4s.service.api.factory.FacebookFactoryInstance;
import it4s.service.api.factory.FacebookInstance;
import it4s.service.api.factory.Factory;
import it4s.service.api.factory.IServiceInstance;

public class OperationFacebook {
	
	private Factory factory;
	private IServiceInstance instance;
	private Facebook facebook;
	private FacebookInstance instanceFacebook;
	
	private static final Logger logger = LoggerFactory
			.getLogger(OperationFacebook.class);
	
	
	public OperationFacebook(){
		instanceFacebook=(FacebookInstance)new FacebookFactoryInstance().createServiceInstance(instance);
		if (instanceFacebook!=null) {
			facebook=instanceFacebook.getFacebook();
		}
	}
	
	public String getMyUser(){
		String userName=null;
		try {
			User user = facebook.getMe();
			userName=user.getName();
		} catch (FacebookException e) {
				throw new IT4SExceptionService(e.getCause(),e.getErrorCode(),e.getErrorMessage());
		}
		return userName;
		
	}
	
	
	public String getNews(){
		String userName=null;
		
			
			ResponseList<Post> feed=null;
			try {
				
				feed = facebook.getHome();
				for (Post post : feed) {
					logger.info(post.getMessage());
				}
			} catch (FacebookException e) {
				throw new IT4SExceptionService(e.getCause(),e.getErrorCode(),e.getErrorMessage());
			}
			
		return userName;
		
	}
	

}
